package com.linck.im.client.handler;

import com.linck.im.client.context.AccountContext;
import com.linck.im.client.context.ChatContext;
import com.linck.im.client.controller.ChatController;
import com.linck.im.model.proto.ProtoMsg;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 聊天消息处理器
 *
 * @author: linck
 * @create: 2021-01-26
 */
public class ClientMessageHandler extends SimpleChannelInboundHandler<ProtoMsg.Message> {

    private static final Logger log = LoggerFactory.getLogger(ClientMessageHandler.class);

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, ProtoMsg.Message message) throws Exception {
        // 收到聊天消息
        log.info("收到聊天消息：\t{}:{} ", message.getFrom(), message.getContent());
        ChatContext.receiveMessage(message);
        if (AccountContext.toAccount.equals(message.getFrom())) {
            ChatController.updateChatView(message.getFrom());
        }
    }
}
